# -*- coding: utf-8 -*-
# flake8: noqa

from qiniu import Auth, put_file, etag,put_data
import qiniu.config

#需要填写你的 Access Key 和 Secret Key
access_key = '0gk91ph-q2uLqtbGgOwtuEW-17SbQpSxHISZJBIm'
secret_key = '4qqMDTY-Slmi3vTxI5DYWRKGxLGd_zVUCyvlABF8'

#构建鉴权对象
q = Auth(access_key, secret_key)

#要上传的空间
bucket_name = 'info'

#上传到七牛后保存的文件名,如果不写，由七牛云维护
# key = 'my-python-logo.png';
key=None;
#生成上传 Token，可以指定过期时间等
token = q.upload_token(bucket_name, key, 3600)
def image_storage(image_data):
    #要上传文件的本地路径
    # localfile = '.person02.png'

    #  判断图片是否上传成功
    ret, info = put_data(token, key, image_data)
    if info.status_code==200:
        return ret.get('key')
    else:
        return ''
    # print(info)
    # print(ret)
    # assert ret['key'] == key
    # assert ret['hash'] == etag(localfile)

if __name__ == '__main__':
    # file=open('person02.png','rb')
    # image_storage(file.read())

    with open('person02.png','rb') as file:
        f=image_storage(file.read())

    print(f)